<template>
	<div>
		<el-dialog :title="'选择'+pType" :visible.sync="dialogTableVisible">
			<div class="departSearch">
				<el-input v-model="input" placeholder="请输入内容" class="pop_input"></el-input>
				<el-button type="success" class="pop_btn" @click="searchFun">搜索</el-button>
				<el-button type="primary" class="pop_btn" @click="sureFun">确认</el-button>
			</div>
			<Graytable :tHead="tHead" :tData="tData" @selectionChange="selectionChange" @blurChange="blurChange"></Graytable>
			<pagination :total="query.total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name:"Poptable",
		data () {
			return {
				dialogTableVisible:false,
				query:{
					total:0,
					page:1,
					limit:10,
				},
				tHead:[],
				tData: [],
				popFlag:true,
				input:"",
				srm:"",
				type:"",
				selectLists:"",
				title:""
			}
		},
		props:{
			pType:{
				required: true,
				type: String
			},
			ctype:{
				type:String,
				default:"mz"
			},
			setting:{
				type:Boolean,
				default:false
			}
		},
		watch :{
			pType (e) {
				this.type=this.pType;
				if(this.type == "科室超时设置"){
					this.title=this.type;
				}else{
					this.title="选择"+this.type;
				}
				this.changeFun();
			},
			ctype(){
				this.changeFun()
			},
			setting(){
				this.changeFun()
			}
		},
		methods:{
			changeFun(){
				this.query.page=1;
				this.query.limit=10;
				this.srm="";
				this.input="";
				this.query.total=0;
				this.tData=[];
				if(this.type == "科室"){
					this.tHead = [{selection:true},{prop:"ksdm",label:"科室编码"},{prop:"ksmc",label:"科室名称"}]
				}
				if(this.type == "疾病"){
					this.tHead = [{selection:true},{prop:"zddm",label:"科室编码"},{prop:"zdmc",label:"科室名称"}]
				}
				if(this.type == "药品"){
					this.tHead = [{selection:true},{prop:"ypdm",label:"药品编码"},{prop:"ypmc",label:"药品名称"},{prop:"ypcj",label:"药厂"},{prop:"ypjx",label:"剂型"},{prop:"ypgg",label:"规格"},{prop:"kfgydw",label:"给药单位"},]
				}
				if(this.type == "医生"){
					this.tHead = [{selection:true},{prop:"ysdm",label:"医生编码"},{prop:"ysmc",label:"医生名称"}]
				}
				if(this.type == "药师"){
					this.tHead = [{selection:true},{prop:"user_name",label:"药师编码"},{prop:"real_name",label:"药师名称"}]
				}
				if(this.type == "科室超时设置"){
					this.tHead = [{label:"序号",index:true},{prop:"deptcode",label:"科室编码"},{prop:"ksmc",label:"科室名称"},{prop:"out_time",label:"超时（可编辑）",input:'int'}]
				}
				this.getList();
			},
			async getList() {
				let res;
				if(this.type == "医生"){
					if(!this.setting){
						res=await this.$ptc.searchDoctor({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
					}else{
						res=await this.$ptc.setDoctorJson({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
					}
				}
				if(this.type == "药品"){
					if(!this.setting){
						res=await this.$ptc.searchDrug({page:this.query.page,pageSize:this.query.limit,srm:this.srm});
					}else{
						res=await this.$ptc.setDrugJson({page:this.query.page,pageSize:this.query.limit,srm:this.srm});
					}
				}
				if(this.type == "药师"){
					res=await this.$ptc.searchPhr({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
				}
				if(this.type == "科室"){
					if(!this.setting){
						res=await this.$ptc.searchDept({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
					}else{
						res=await this.$ptc.setDeptJson({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
					}
				}
				if(this.type == "疾病"){
					if(this.setting){
						res=await this.$ptc.setDiseaseJson({page:this.query.page,pageSize:this.query.limit,srm:this.srm});
					}
				}
				if(this.type == "科室超时设置"){
					res=await this.$ptc.setDeptoutJson({page:this.query.page,pageSize:this.query.limit,srm:this.srm,type:this.ctype});
				}
				this.query.total=res.total;
				this.tData=res.rows;
			},
			show(){
				this.dialogTableVisible = true
			},
			hide(){
				this.dialogTableVisible = false
			},
			searchFun(){
				this.query={
					page:1,
					limit:10,
					total:0
				}
				this.srm=this.input;
				this.getList();
			},
			selectionChange(data){
				this.selectLists=data;
			},
			sureFun(){
				if(this.type=="科室"){
					this.$emit("dept",this.selectLists)
				}
				if(this.type=="医生"){
					this.$emit("doc",this.selectLists)
				}
				if(this.type=="药师"){
					this.$emit("phr",this.selectLists)
				}
				if(this.type=="药品"){
					this.$emit("drug",this.selectLists)
				}
				if(this.type =="疾病"){
					this.$emit("disease",this.selectLists)
				}
				this.hide();
			},
			async blurChange(prop,row){
				const loading = this.$loading({
				  lock: true,
				  text: '正在处理中，请稍后',
				  spinner: 'el-icon-loading',
				  background: 'rgba(0, 0, 0, 0.7)'
				});
				let res=await this.$ptc.setDeptoutEdit({
					deptcode:row.deptcode,
					type:this.ctype,
					ksdm_encode: row.ksdm_encode,
					out_time: row[prop],
					ksmc: row.ksmc
				})
				loading.close();
				this.$message({
				  message: res.msg,
				  type: res.code == 1?'success':'error',
				  duration:1500
				});
				if(res.code == 1){
					this.getList();
				}
			}
		}
	}
</script>

<style>
</style>
